package com.spring.demo.publicutil.thread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * CyclicBarrier
 * @author youjc
 */
public class T04_CyclicBarrier4 {
    public static void main(String[] args) {
        //    CyclicBarrier cyclicBarrier = new CyclicBarrier(20);
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5, () -> System.out.println("满人了"));
/*    CyclicBarrier cyclicBarrier = new CyclicBarrier(20, new Runnable() {
        @Override
        public void run() {
            System.out.println("满人了，发车");
        }
    });*/
        for (int i = 0; i < 100; i++) {
            int finalI=i;
            new Thread(()->{
                try {
                    System.out.println(finalI);
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();

        }
    }


}
